package com.gl.rent;

import com.gl.common.util.text.Convert;
import com.haixiaoke.saas.repository.core.AjaxResult;
import com.haixiaoke.saas.repository.core.BaseController;
import com.haixiaoke.saas.repository.core.page.TableDataInfo;
import com.haixiaoke.saas.repository.pojo.RecommendItem;
import com.haixiaoke.saas.repository.service.IRecommendItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 推荐房源Controller
 *
 * @author zhiping
 * @date 2023-04-18
 */
@RestController
@RequestMapping("/recommend/recommendItem")
public class RecommendItemController extends BaseController {
    @Autowired
    private IRecommendItemService recommendItemService;

    /**
     * 查询推荐房源列表
     */
    @GetMapping("/list")
    public TableDataInfo list(RecommendItem recommendItem) {
        startPage();
        List<RecommendItem> list = recommendItemService.selectRecommendItemList(recommendItem);
        return getDataTable(list);
    }


    /**
     * 获取推荐房源详细信息
     */
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId) {
        return success(recommendItemService.selectRecommendItemByItemId(itemId));
    }

    /**
     * 新增推荐房源
     */
    @PostMapping
    public AjaxResult add(@RequestBody RecommendItem recommendItem) {
        return toAjax(recommendItemService.insertRecommendItem(recommendItem));
    }

    /**
     * 批量新增推荐房源
     *
     * @return
     */
    @PostMapping("/batchAdd")
    public AjaxResult batchAdd(@RequestBody Map<String, Object> map) {
        List<RecommendItem> items = new ArrayList<>();

        Long groupId = Convert.toLong(map.get("groupId"));
        List<String> houseIdArr = (ArrayList) map.get("houseIds");

        for (String houseId : houseIdArr) {
            RecommendItem recommendItem = new RecommendItem();
            recommendItem.setGroupId(groupId);
            recommendItem.setHouseId(houseId);
            items.add(recommendItem);
        }
        return toAjax(recommendItemService.batchInsertRecommendItems(items));
    }

    /**
     * 修改推荐房源
     */
    @PutMapping
    public AjaxResult edit(@RequestBody RecommendItem recommendItem) {
        return toAjax(recommendItemService.updateRecommendItem(recommendItem));
    }

    /**
     * 删除推荐房源
     */
    @DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds) {
        return toAjax(recommendItemService.deleteRecommendItemByItemIds(itemIds));
    }
}
